<?php
defined ( 'SYSPATH' ) or die ( 'No direct access allowed.' );
/**
 *
 *收货报表
 * 
 * 
 * @author cxy
 *        
 */
class Controller_receiptreport extends Template
{
    public function before()
    {
    	$userid=User::active_user()->id;
		$this->user=$userid;
       	$username=ORM::factory('user')->where('id', '=', $userid)->find();
    	$this->recieve_man= $username->name;
    	try {
    		  ACL::Required('receiptreport');
    	} catch ( HTTP_Exception_403 $e ) {
				Message::error('sorry，你没有权限进这个目录');
    		$this->request->redirect ( URL::base (true) );
    	}
        parent::before();
    }
    public function action_select_receipt()
    {    
    	$this->title='收货详情报表';
    	$db=DB::select()->from('recieve_details')
    	->join('recieve_invoices')
    	->on('recieve_invoices.id','=','recieve_details.recieve_invoice_id');
    	if(isset($_GET['date_from']) and trim($_GET['date_from']))
    	{
    		$date_from=trim($_GET['date_from']);
			$t=strtotime($date_from);
			$date_from=date("Y-m-d H:i:s",$t);				
			$db->where('recieve_invoices.recieve_time', '>', "$date_from");
    	}
    	if(isset($_GET['date_to']) and trim($_GET['date_to']))
		{				
			$date_to_1=trim($_GET['date_to']);			
			$t=strtotime($date_to_1);
			$date_to=date("Y-m-d H:i:s",$t+24*3600);				
			$db->where('recieve_invoices.recieve_time', '<', "$date_to");
		}
		if(isset($_GET['supplier']) and trim($_GET['supplier']))
		{
			$supplier=trim($_GET['supplier']);
			$supplier=basic::getidByName($supplier);	
			$db->where('recieve_invoices.supplier', '=', $supplier);	
		}
		if(isset($_GET['purchaseorder_id']) and trim($_GET['purchaseorder_id']))
		{
			$purchaseorder_id=trim($_GET['purchaseorder_id']);
			$db->where('recieve_invoices.purchaseorder_id', '=', $supplier);	
		}
    	if(isset($_GET['sku']) and trim($_GET['sku']))
		{
			$purchaseorder_id=trim($_GET['sku']);
			$db->where('recieve_details.sku', '=', $supplier);	
		}
    	$str=$db->execute('newerp')->as_array();
    	$count=count($str);
    	$pagination=Pagination::factory(array(
			'total_items'=>$count,//总记录数
			'items_per_page' => 5,//每页显示多少条	
			'uri'                => $this->request->uri(),	
				));				
			$db->limit($pagination->items_per_page);
			$db->offset($pagination->_offset);
			$result=$db->execute('newerp')->as_array();
			
//    	echo '<pre>';
//    	print_r($result);
//    	echo '</pre>';
//    	echo $count;
    	
    	$view=View::factory('receipt/report');
    	$view->result=$result;
    	$view->pagination=$pagination;
    	$view->purchaseorder_id = isset($_GET['purchaseorder_id']) ? $_GET['purchaseorder_id']: NULL;
		$view->supplier = isset($_GET['supplier']) ? $_GET['supplier']: NULL;

		$view->date_from = isset($_GET['date_from']) ? $_GET['date_from']: NULL;
		$view->date_to = isset($_GET['date_to']) ? $_GET['date_to']: NULL;

		$view->status = isset($_GET['sku']) ? $_GET['sku']: NULL;
    	$this->response->body($view);
    	
    }
}